#include"Mylog.h"

#include<iostream>

#include<log4cpp/Priority.hh>
#include<log4cpp/PatternLayout.hh>
#include<log4cpp/OstreamAppender.hh>
#include<log4cpp/FileAppender.hh>

using std::cout;
using std::endl;

Mylog * Mylog::_pInstance=nullptr;  //初始化

Mylog* Mylog::getInstance(){
    if(nullptr==_pInstance){
        _pInstance=new Mylog();
    }
    return _pInstance;
}
void Mylog::destroy(){
    if(_pInstance){
        delete _pInstance;
        _pInstance=nullptr;
    }
}

void Mylog::error(const string &str){
    _mylog.error(str);
}
void Mylog::warn (const string &str){
    _mylog.warn(str);
}
void Mylog::info (const string &str){
    _mylog.info(str);
}
void Mylog::debug (const string &str){
    _mylog.debug(str);
}

Mylog::Mylog()
:_mylog(Category::getRoot().getInstance("mylog")){
    //ptnLayout1 为测试使用，输出为屏幕，非日志文件
    //auto ptnLayout1= new PatternLayout();
    auto ptnLayout2= new PatternLayout();
    //ptnLayout1->setConversionPattern("%d %c [%p] %m%n");    
    ptnLayout2->setConversionPattern("%d %c [%p] %m%n");    //格式： 年-月-日 时-分-秒，？？（中间数字不知道啥）

    //auto pOstreamApp=new OstreamAppender("console",&cout);
    auto pFileApp=new FileAppender("fileApp","Search_Engine.log");  //第二个为输出文件名
    //pOstreamApp->setLayout(ptnLayout1);
    pFileApp->setLayout(ptnLayout2);

    //_mylog.addAppender(pOstreamApp);
    _mylog.addAppender(pFileApp);
    _mylog.setPriority(Priority::DEBUG);

    //cout<<"Mylog()"<<endl;
}

Mylog::~Mylog(){
    //cout<<"~Mylog()"<<endl;
    Category::shutdown();
}


